#coding = utf-8
import requests
import re
import time
import random

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    'Upgrade-Insecure-Requests': '1',
}

read_url = 'https://www.piaotian.com/html/1/1710/'
req = requests.get(read_url,headers = headers)
urls = re.findall(r'<li><a href="(.*?)">',req.text,re.S)
root = 'E://pics//'
name = 0
for i in urls:
    if len(i) < 15 :
        url = 'https://www.piaotian.com/html/1/1710/'+str(i)
        try:
            req_1 = requests.get(url,headers = headers)
        except TimeoutError:
            req_1 = requests.get(url, headers=headers)
        title = re.findall(r'<H1><a href="http://www.piaotian.com/bookinfo/1/1710.html">最强弃少</a>(.*?)</H1>',req_1.text,re.S)
        read_strat = re.findall(r'</td></tr></table></td></tr></table>\r\n<br>(.*?)</div>',req_1.text,re.S)
        read_str = str(read_strat).replace('&nbsp;','').replace('<br />','').replace(r'\r\n','\r\n')
        path = root+str(title)+'.txt'
        file = open(path,'w')
        file.write(read_str.replace(u'\ufffd', u' '))
        file.close()
        name += 1
        print('第'+str(name)+'章保存成功')
        time.sleep(random.randint(1, 200)/400)


